//app.js
import {
    ToastPannel
} from './component/toast/toast'
const config = require('./config.js');
const util = require('./utils/util.js');
const ald = require('./sdk/statis/ald-stat.js')
import { getMemberInfo } from './static/api/api.js'
App({
    ToastPannel,
    onLaunch: function () {
        let that = this;
        //调用API从本地缓存中获取数据
        var logs = wx.getStorageSync('logs') || []
        that.getSysInfo()

    },
    onShow: function (options) {
        let that = this;
        let params = decodeURIComponent(options.query.scene);
        console.log('二维码参数')
        wx.setStorageSync('is_author', false)
        //获取拓展码
        that.globalData.promote_store_id = params;
        that.getUserInfo()
    },
    getUserInfo: function (cb) {
        var that = this
        let flag = false;
        if (wx.getStorageSync('token')) flag = true;
        if (that.globalData.promote_store_id) flag = false;
        if (flag) {
            console.log('未授权登录回调')
            typeof cb == "function" && cb(this.globalData.userInfo)
        } else {
            //调用登录接口
            wx.login({
                success: function (ev) {
                    console.log('登录成功')
                    console.log(ev)
                    that.globalData.ev = ev;
                    wx.getUserInfo({
                        withCredentials: true,
                        success: function (res) {
                            console.log('获取用户信息unionid')
                            console.log(res);
                            wx.request({
                                url: config.openIdUrl,
                                method: 'POST',
                                header: {
                                    'site': config.site,
                                    'os': config.os,
                                    'token': wx.getStorageSync('token'),
                                    
                                },
                                data: {
                                    code: ev.code,
                                    promote_code: that.globalData.promote_code,
                                    encryptedData: res.encryptedData,
                                    iv: res.iv,
                                    signature: res.signature,
                                    rawData: res.rawData,
                                    type: 2

                                },
                                success: function (res) {
                                    console.log('token:' + res.data.data.session_key)
                                    wx.setStorageSync('token', res.data.data.session_key)
                                    wx.setStorageSync('unionid', res.data.data.unionid)
                                    if (res.data.data.session_key != '') {
                                        typeof cb == "function" && cb(res.data.data)
                                    }

                                }
                            })
                        },
                        fail: function () {
                            // wx.showModal({
                            //     title: '警告',
                            //     content: '尚未进行授权，请点击确定跳转到授权页面进行授权。',
                            //     success: function (res) {
                            //         if (res.confirm) {
                            //             console.log('用户点击确定')
                            //             wx.navigateTo({
                            //                 url: '/pages/author_login/author_login',
                            //             })
                            //         }
                            //     }
                            // })
                        }
                    })
                }
            })
        }
    },
    //获取用户信息
    _getUserInfo(cb) {
        let that = this
        wx.showLoading({
            title: '加载中...',
        })
        getMemberInfo().then(response => {
            wx.hideLoading();
            console.log('请求成功')
            
            that.globalData.memberInfo = response.data;
            typeof cb == "function" && cb(response.data)
        }).catch(response => {
            wx.hideLoading();
            console.log(response)
        })
    },
    //获取窗口高度
    getSysInfo() {
        var that = this
        // 获取系统信息
        wx.getSystemInfo({
            success: function (res) {
                that.globalData.sysInfo = res;
            }
        })
    },



    globalData: {
        storeId: '',
        userInfo: null,
        memberInfo: null,
        sysInfo: null,
        promote_code: null,
        ev: null,
        WebSiteConfig: null,
    }
})